<template>
  <div>
    <a-modal v-model:visible="pageData.visible" width="60%" :title="pageData.title" :destroyOnClose="true" @ok="clickModalOk" @cancel="handleModalCancel">
      <a-form ref="postAddUpdateFormRef" name="postAddUpdateForm" :model="postAddUpdateForm.data" :labelCol="{ span: 8 }" :wrapperCol="{ span: 14 }">
        <a-row :gutter="24">
        <a-col span="12">
            <a-form-item name="postCode" label="岗位编码" :rules="[{ required: true, message: '岗位编码不能为空！' }]">
              <a-input v-model:value="postAddUpdateForm.data.postCode" :maxlength="64" placeholder="请填写岗位编码" ></a-input>
            </a-form-item>
          </a-col>
        <a-col span="12">
            <a-form-item name="postName" label="岗位名称" :rules="[{ required: true, message: '岗位名称不能为空！' }]">
              <a-input v-model:value="postAddUpdateForm.data.postName" :maxlength="50" placeholder="请填写岗位名称" ></a-input>
            </a-form-item>
          </a-col>
        <a-col span="12">
            <a-form-item name="postSort" label="显示顺序" :rules="[{ required: true, message: '显示顺序不能为空！' }]">
              <a-input v-model:value="postAddUpdateForm.data.postSort" :maxlength="4" placeholder="请填写显示顺序" ></a-input>
            </a-form-item>
          </a-col>
        <a-col span="12">
            <a-form-item name="status" label="岗位状态" :rules="[{ required: true, message: '岗位状态不能为空！' }]">
              <a-radio-group v-model:value="postAddUpdateForm.data.status" :options="fatherPageData.sysNormalDisableDict" optionType="button" button-style="solid"></a-radio-group>
            </a-form-item>
          </a-col>
        <a-col span="12">
            <a-form-item name="remark" label="备注" :rules="[{}]">
          <a-textarea v-model:value="postAddUpdateForm.data.remark" :maxlength="500" :rows="3" placeholder="请填写备注"  />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-modal>
  </div>
</template>
<script setup>
import { reactive, ref } from 'vue';
import AutoeeUtil from '@/utils/AutoeeUtil.js';
import { getPost,  addPost, updatePost } from "@/api/system/post";
import { message } from 'ant-design-vue';

// 父页面公用数据
const fatherPageData = defineProps({
  sysNormalDisableDict: Array,
});

// 引入父页面方法
const fatherPageFunEmit = defineEmits([]);

// 当前页面使用的数据
const pageData = reactive({
  title: '新增页面',
  visible: false,
  operateType: '',
});

// 新增修改Form
const postAddUpdateFormRef = ref();
const postAddUpdateForm = reactive({ data: {} });
// 重置Form
const resetPostAddUpdateForm = () => {
  AutoeeUtil.resetFormFieldsToEmpty(postAddUpdateForm.data);
};

let fatherPageTablePage;
// 打开新增窗口
const openAddModal = (pFatherPageTablePage) => {
  pageData.operateType = 'add';
  pageData.title = '新增';
  fatherPageTablePage = pFatherPageTablePage;
  pageData.visible = true;
};

// 打开修改窗口
const openUpdateModal = (record, tablePage) => {
  pageData.operateType = 'update';
  pageData.title = '修改';
  fatherPageTablePage = tablePage;
  getPost(record.postId).then((response) => {
    postAddUpdateForm.data = response.data;
    pageData.visible = true;
  });
};

// 点击窗口确认
const clickModalOk = (e) => {
  postAddUpdateFormRef.value
    .validateFields()
    .then((values) => {
      if (pageData.operateType === 'add') {
        addPost(postAddUpdateForm.data).then((res) => {
          pageData.visible = false;
          resetPostAddUpdateForm();
          fatherPageTablePage.reload();
          AutoeeUtil.messageSuccess('新增操作成功。');
        });
      } else if (pageData.operateType === 'update') {
        updatePost(postAddUpdateForm.data).then((res) => {
          pageData.visible = false;
          resetPostAddUpdateForm();
          fatherPageTablePage.reload();
          AutoeeUtil.messageSuccess('修改操作成功。');
        });
      }
    })
    .catch((info) => {
      console.log('Validate Failed:', info);
    });
};

// 点击窗口取消
const handleModalCancel = () => {
  resetPostAddUpdateForm();
};

// 对外暴露出去
defineExpose({
  openAddModal,
  openUpdateModal,
});
</script>

<style lang="less"></style>
